package com.huluobo.a2107monthdemo.cart

import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.huluobo.a2107monthdemo.base.BaseViewModel
import com.huluobo.a2107monthdemo.net.Const
import okhttp3.MediaType
import okhttp3.RequestBody

/**
 *  Created by LC on 2023/12/1.
 */
class CartViewModel : BaseViewModel() {
    private val repo = CartRepo()
    val getCartListSuccess = MutableLiveData<CartBeanResult>()
    val getCartListFailed = MutableLiveData<String>()

    val deleteCartSuccess = MutableLiveData<ChangeCartBeanResult>()
    val deleteCartFailed = MutableLiveData<String>()

    val addSuccess = MutableLiveData<ChangeCartBeanResult>()
    val addFailed = MutableLiveData<String>()
    val removeSuccess = MutableLiveData<ChangeCartBeanResult>()
    val removeFailed = MutableLiveData<String>()

    fun getCartList() {
        repo.getCartList(getCartListSuccess, getCartListFailed)
    }

    fun deleteCart(ids: String) {
        val map = mutableMapOf<String, String>()
        map[Const.PARAM_IDS] = ids
        val json = Gson().toJson(map)
        val body = RequestBody.create(MediaType.parse(Const.MEDIA_PARSE), json)
        repo.deleteCart(body, deleteCartSuccess, deleteCartFailed)
    }

    fun addCart(count: Int, goodsId: Int) {
        val map = mutableMapOf<String, Int>()
        map[Const.PARAM_COUNT] = count
        map[Const.PARAM_GOODS_ID] = goodsId
        val json = Gson().toJson(map)
        val body = RequestBody.create(MediaType.parse(Const.MEDIA_PARSE), json)
        repo.changeCartNum(body, addSuccess, addFailed)
    }

    fun removeCart(count: Int, goodsId: Int) {
        val map = mutableMapOf<String, Int>()
        map[Const.PARAM_COUNT] = count
        map[Const.PARAM_GOODS_ID] = goodsId
        val json = Gson().toJson(map)
        val body = RequestBody.create(MediaType.parse(Const.MEDIA_PARSE), json)
        repo.changeCartNum(body, removeSuccess, removeFailed)
    }
}